package com.example.tiantian.battery;

import com.example.tiantian.BaseActivity;
import com.example.tiantian.OnReceiveListener;
import com.example.tiantian.R;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class BatteryInfoActivity extends BaseActivity implements OnReceiveListener {

    static final int COLOR_SAFE = Color.GREEN;
    static final int COLOR_WARNING = Color.YELLOW;
    static final int COLOR_DANGER = Color.RED;

    ImageView imgBatteryLevel;
    TextView txtBatteryLevel;
    TextView txtBatteryChargeState;
    TextView txtBatteryHealthState;
    TextView txtbatteryTechnology;
    TextView txtBatteryVoltage;
    TextView txtBatteryTemperature;

    BatteryReceiver receiver;
    IntentFilter filter;

    @Override
    protected void init() {
        setContentView(R.layout.activity_battery_info);

        imgBatteryLevel = (ImageView) findViewById(R.id.battery_info_image);
        txtBatteryLevel = (TextView) findViewById(R.id.battery_info_level);
        txtBatteryChargeState = (TextView) findViewById(R.id.battery_info_state_charge);
        txtBatteryHealthState = (TextView) findViewById(R.id.battery_info_state_health);
        txtbatteryTechnology = (TextView) findViewById(R.id.battery_info_technology);
        txtBatteryVoltage = (TextView) findViewById(R.id.battery_info_voltage);
        txtBatteryTemperature = (TextView) findViewById(R.id.battery_info_temperature);

        receiver = new BatteryReceiver();
        filter = new IntentFilter();
    }

    @Override
    protected void loadData() {
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
    }

    @Override
    protected void event() {
        receiver.SetOnReceiveListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(receiver, filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        int batteryLevel = extras.getInt(BatteryManager.EXTRA_LEVEL, 0);
        int voltage = extras.getInt(BatteryManager.EXTRA_VOLTAGE, 0);
        float batteryVoltage = voltage / 1000.0f;
        int temperature = extras.getInt(BatteryManager.EXTRA_TEMPERATURE, 0);
        float batteryTemperature = temperature / 10.0f;
        int batteryStateCharge = extras.getInt(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
        int batteryStateHealth = extras.getInt(BatteryManager.EXTRA_HEALTH, BatteryManager.BATTERY_HEALTH_UNKNOWN);

        String batteryTechnology = extras.getString(BatteryManager.EXTRA_TECHNOLOGY, "UNKNOWN");

        txtbatteryTechnology.setText(batteryTechnology);
        txtbatteryTechnology.setTextColor(COLOR_SAFE);

        txtBatteryTemperature.setText(batteryTemperature + "℃");
        txtBatteryTemperature.setTextColor(
                batteryTemperature > 50 ? COLOR_DANGER : (batteryTemperature > 40 ? COLOR_WARNING : COLOR_SAFE));

        txtBatteryVoltage.setText(batteryVoltage + "V");
        txtBatteryVoltage.setTextColor(COLOR_SAFE);

        if (batteryLevel == 100) {
            imgBatteryLevel.setImageResource(R.drawable.bt100);
        } else if (batteryLevel >= 90) {
            imgBatteryLevel.setImageResource(R.drawable.bt90);
        } else if (batteryLevel >= 80) {
            imgBatteryLevel.setImageResource(R.drawable.bt80);
        } else if (batteryLevel >= 70) {
            imgBatteryLevel.setImageResource(R.drawable.bt70);
        } else if (batteryLevel >= 50) {
            imgBatteryLevel.setImageResource(R.drawable.bt50);
        } else if (batteryLevel >= 30) {
            imgBatteryLevel.setImageResource(R.drawable.bt30);
        } else if (batteryLevel >= 20) {
            imgBatteryLevel.setImageResource(R.drawable.bt20);
        } else if (batteryLevel >= 10) {
            imgBatteryLevel.setImageResource(R.drawable.bt10);
        } else {
            imgBatteryLevel.setImageResource(R.drawable.bt0);
        }

        txtBatteryLevel.setText(batteryLevel + "%");
        txtBatteryLevel
                .setTextColor(batteryLevel >= 70 ? COLOR_SAFE : (batteryLevel >= 20) ? COLOR_WARNING : COLOR_DANGER);

        switch (batteryStateCharge) {
        case BatteryManager.BATTERY_STATUS_CHARGING:
            txtBatteryChargeState.setText(R.string.battery_status_chaging);
            txtBatteryChargeState.setTextColor(COLOR_SAFE);
            break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
            txtBatteryChargeState.setText(R.string.battery_status_discharging);
            txtBatteryChargeState.setTextColor(COLOR_SAFE);
            break;
        case BatteryManager.BATTERY_STATUS_FULL:
            txtBatteryChargeState.setText(R.string.battery_status_full);
            txtBatteryChargeState.setTextColor(COLOR_SAFE);
            break;
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
            txtBatteryChargeState.setText(R.string.battery_status_not_charging);
            txtBatteryChargeState.setTextColor(COLOR_WARNING);
            break;
        case BatteryManager.BATTERY_STATUS_UNKNOWN:
            txtBatteryChargeState.setText(R.string.battery_unknown);
            txtBatteryChargeState.setTextColor(COLOR_DANGER);
            break;
        default:
            break;
        }

        switch (batteryStateHealth) {
        case BatteryManager.BATTERY_HEALTH_GOOD:
            txtBatteryHealthState.setText(R.string.battery_health_good);
            txtBatteryHealthState.setTextColor(COLOR_SAFE);
            break;
        case BatteryManager.BATTERY_HEALTH_OVERHEAT:
            txtBatteryHealthState.setText(R.string.battery_health_overheat);
            txtBatteryHealthState.setTextColor(COLOR_WARNING);
            break;
        case BatteryManager.BATTERY_HEALTH_DEAD:
            txtBatteryHealthState.setText(R.string.battery_health_dead);
            txtBatteryHealthState.setTextColor(COLOR_DANGER);
            break;
        case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
            txtBatteryHealthState.setText(R.string.battery_health_over_voltage);
            txtBatteryHealthState.setTextColor(COLOR_WARNING);
            break;
        case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
            txtBatteryHealthState.setText(R.string.battery_health_unspecified_failure);
            txtBatteryHealthState.setTextColor(COLOR_DANGER);
            break;
        case BatteryManager.BATTERY_HEALTH_UNKNOWN:
            txtBatteryHealthState.setText(R.string.battery_unknown);
            txtBatteryHealthState.setTextColor(COLOR_DANGER);
            break;
        default:
            break;
        }
    }

}
